package com.mg.message.controller;

import com.mg.common.vo.ResponseResult;
import com.mg.message.entity.Message;
import com.mg.message.service.MessageService;
import com.mg.message.service.UserService;
import com.mg.message.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/message")
public class MessageController {
    @Autowired
    private MessageService messageService;
    @Autowired
    private UserService userService;

    /**
     * 短信总览，分页显示
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/getMessage")
    public ResponseResult<PageUtils<List<Message>>> getMessages(@RequestParam(value = "page", defaultValue = "1") int currentPage, @RequestParam(value = "size",defaultValue = "10") int pageSize) {
        return messageService.getMessages(currentPage, pageSize);
    }

    /**
     * 创建短信
     * @param requestMessage
     * @return
     */
    @PostMapping("/createMessage")
    public ResponseResult<Integer> createMessage(@RequestBody RequestMessage requestMessage) {
        return messageService.createMessage(requestMessage);
    }

    /**
     * 群发短信：限制发送的用户群
     * @param requestSelectUser
     * @return
     */
    @PostMapping("/getUsers")
    public ResponseResult<UsersResponse> getMessageUsers(@RequestBody RequestSelectUser requestSelectUser) {
        ResponseResult<UsersResponse> usersByConditions = userService.getUsersByConditions(requestSelectUser);
        return usersByConditions;
    }

    /**
     * 查看短信详情
     * @param id
     * @return
     */
    @GetMapping("/getDetail")
    public ResponseResult<RequestMessage> getMessageDetail(@RequestParam int id) {
        return messageService.getMessageDetail(id);
    }

    /**
     * 更新短信的信息（注意：已经发送的不能更新，即审核通过状态）
     * @param requestMessage
     * @return
     */
    @PostMapping("/updateDetail")
    public ResponseResult<Integer> updateMessage(@RequestBody RequestMessage requestMessage) {
        return messageService.updateMessage(requestMessage);
    }

    /**
     * 删除单条短信（设置假删除，已经发送的短信无法删除，如果有定时发送，连同定时器一起删除）
     */
    @DeleteMapping("/delMessage")
    public ResponseResult<Integer> delMessage(@RequestParam("id") int mid) {
        return messageService.delMessage(mid);
    }
}
